package com.example.dialog.Tools;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

//该工具类重写了SQLiteOpenHelper类，用于保存文章细节信息（如标题/作者信息等）
public class DatabaseHelperForSearchDetails extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "searchdatabase.db";    //改文件名点①
    private static final int DATABASE_VERSION = 1;

    public DatabaseHelperForSearchDetails(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
//        String CREATE_TABLE = "CREATE TABLE searchdatabase ("   //改文件名点②
//                + "id INTEGER PRIMARY KEY AUTOINCREMENT, "
//                + "searchText TEXT)";
//        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public List<ArticleInfo> querySearchDataBase() {
        List<ArticleInfo> searchResultList = new ArrayList<>();
        String selectQuery = "SELECT title, usrImage, usrName, mtv_summary, contentImage, mtv_content, mtv_data_read, mtv_data_like, mtv_data_star FROM articleinfo"; // 确保查询语句正确

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                String title = cursor.getString(cursor.getColumnIndex("title"));
                String usrImage = cursor.getString(cursor.getColumnIndex("usrImage"));
                String usrName = cursor.getString(cursor.getColumnIndex("usrName"));
                String mtv_summary = cursor.getString(cursor.getColumnIndex("mtv_summary"));
                String contentImage = cursor.getString(cursor.getColumnIndex("contentImage"));
                String mtv_content = cursor.getString(cursor.getColumnIndex("mtv_content"));
                String mtv_data_read = cursor.getString(cursor.getColumnIndex("mtv_data_read"));
                String mtv_data_like = cursor.getString(cursor.getColumnIndex("mtv_data_like"));
                String mtv_data_star = cursor.getString(cursor.getColumnIndex("mtv_data_star"));
                ArticleInfo articleInfo = new ArticleInfo(title, usrImage, usrName, mtv_summary, contentImage, mtv_content, mtv_data_read, mtv_data_like, mtv_data_star);
                searchResultList.add(articleInfo);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return searchResultList;
    }
}
